package com.rongshu.view.home.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.DateUtils;
import com.rongshu.view.home.entity.PeopleRecommendRespose;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

public class PeopleRecommendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<PeopleRecommendRespose.ResponseBean> responseBeanList = null;

    public void setResponseBeanList(List<PeopleRecommendRespose.ResponseBean> list) {
        this.responseBeanList = list;
    }

    public void addResponseBeanList(List<PeopleRecommendRespose.ResponseBean> list) {
        this.responseBeanList.addAll(list);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_people_recommend_item, parent, false);
        return new PeopleRecommendViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        PeopleRecommendRespose.ResponseBean responseBean = responseBeanList.get(position);
        PeopleRecommendViewHolder viewHolder = (PeopleRecommendViewHolder) holder;
        try {
            String content = responseBean.getContents().replaceAll("%(?![0-9a-fA-F]{2})", "%25");
            viewHolder.txt_article.setText(URLDecoder.decode(content, "utf-8"));
            viewHolder.txt_article_data.setText(DateUtils.ConvertDate(responseBean.getCreate_datetime()));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        if (responseBeanList != null) {
            return responseBeanList.size();
        }
        return 0;
    }

    public static class PeopleRecommendViewHolder extends RecyclerView.ViewHolder {

        private TextView txt_article, txt_article_data;

        public PeopleRecommendViewHolder(View itemView) {
            super(itemView);
            txt_article = itemView.findViewById(R.id.txt_article);
            txt_article_data = itemView.findViewById(R.id.txt_article_data);
        }
    }
}
